
console.log('GetinfoPopup插件注入成功')



//监听
var ReplyMessage;
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {

    console.log(message, "message")
    // 在这里处理接收到的消息
    ReplyMessage = message

    // //判断有没有登录
    if (message.page == "GetLoginPage") {
        document.querySelector(".login_box").classList.remove("active");
        console.log("走登录")
    }
    else if (message.page == "AddInfoPage") {
        let selection = window.getSelection();
        if (selection.rangeCount > 0) {
            let selectedText = selection.toString();
            document.querySelector(".add_box").classList.remove("active")
            //  域名
            document.querySelector("#Domain").value = message.domainName

            //根据名称 获取 到 内容 选中
            InfoAddSelectPage(message.dataType)

            //数据内容
            document.querySelector("#ContactValue").value = selectedText
        }
    }
    else if (message.page == "GetInfoPage") {

        document.querySelector(".GetInfo_box").classList.remove("active")
        console.log("已经登录 走保存")
        //保存页面
        IsFlagRender(message)
    }
});



















//内容选中  单选框点击事件
function InfoAddSelectPage(type) {
    for (let i = 0; i < document.querySelectorAll(".radio_item").length; i++) {
        if (document.querySelectorAll(".radio_item")[i].querySelector("span").innerText == type) {
            document.querySelectorAll(".radio_item")[i].querySelector(".radioSelect").checked = true
        }
    }

    for (let j = 0; j < document.querySelectorAll(".radio_item").length; j++) {
        document.querySelectorAll(".radio_item")[j].onclick = function () {
            for (let i = 0; i < document.querySelectorAll(".radio_item").length; i++) {
                document.querySelectorAll(".radio_item")[i].querySelector(".radioSelect").checked = false
            }
            document.querySelectorAll(".radio_item")[j].querySelector(".radioSelect").checked = true
        }
    }

}


//单个保存点击事件
function SaveOnceData() {
    //数据获取页面的单个保存
    var SaveEmailButton = document.querySelectorAll(".Page_Contact_Information_content_item_content_button")
    for (let i = 0; i < SaveEmailButton.length; i++) {
        SaveEmailButton[i].onclick = function () {
            //单个保存
            let Email = document.querySelectorAll(".Page_Contact_Information_content_item_content_input")[i].querySelector("input").value;
            let type = document.querySelectorAll(".Page_Contact_Information_content_item")[0].querySelector(".Page_Contact_Information_content_item_title").innerText


            chrome.storage.local.get('TopeasyAccountCode', async function (result) {

                if (result.TopeasyAccountCode != null && result.TopeasyAccountCode != "undefined") {
                    // //请求数据
                    let requestdata = {
                        "id": "",
                        "AccountCode": result.TopeasyAccountCode,
                        "Domain": window.location.href.split("://")[1].split("/")[0],
                        "Protocol": window.location.href.split("://")[0],
                        "Trip": "",
                        "DomainContactWareHouseList": [
                            {
                                "Id": "",
                                "DomainId": "",
                                "ContactType": type,
                                "ContactValue": Email,
                                "SourceUrl": window.location.href,//.split("://")[1].split("/")[0],
                                "SourceType": "0",
                                "Trip": ""
                            }
                        ]
                    }
                    //如果没有 就去登录页面
                    //直接保存页面
                    let data = {
                        type: "saveData",
                        mAccountCode: EncodeStrXor(result.TopeasyAccountCode),
                        data: requestdata
                    }

                    await chrome.runtime.sendMessage({ "data": data }, function (response) {
                        console.log(response, "responseresponseresponseresponse")
                        // //需要一个消息提示 保存完毕y
                        try {
                            if (response.status == 1) {
                                console.log("保存成功")
                                ChromePopUp("保存成功")
                            } else {
                                ChromePopUp("保存失败")
                                console.log("保存失败");
                            }
                            console.log(response, "单个保存返回")
                        } catch (error) {
                            console.log("返回错误")
                        }
                    });

                } else {
                    //去登陆
                    console.log("没登陆")

                    document.querySelector(".login_box").classList.remove("active");
                }
            });
        }
    }
}



